package cz.zcu.fav.ratdroid.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;


import android.util.SparseArray;

/**
 * This is a serializable representation of all days IN SINGLE MONTH which are
 * occupied by user reservations.
 * 
 * 
 * According to documentation its not possible to use custom converter to array
 * of values in simplexml library - so we need to do it on ourself.
 * 
 * Please do not use this object for anything else than keeping dates from
 * single month - its internally optimized for that purpose and any other usage
 * might not work.
 * 
 * @author Lukas Gemela
 * 
 */
@Root(name = "occupations")
public class Occupations implements Serializable
{
	private static final long serialVersionUID = -2977343711042442321L;

	@ElementList(entry = "occupation", inline = true, required = false)
	List<Occupation> occupations = new ArrayList<Occupation>();
	//for better optimalisation covvert everything to hashmap
	private SerializableSparseArray<Occupation> hashOcc;

	public Occupation getOccupation(int dayOfMonth)
	{
		if (hashOcc == null)
		{
			createHashMap();

		}
		return hashOcc.get(dayOfMonth);
	}

	private void createHashMap()
	{
		hashOcc = new SerializableSparseArray<Occupation>();
		if (occupations != null)
		{
			for (Occupation occ : occupations)
			{
				hashOcc.put(occ.getDate().getDayOfMonth(), occ);
			}
		}
	}

	class SerializableSparseArray<E> extends SparseArray<E> implements
			Serializable
	{
		private static final long serialVersionUID = 5388757581035942403L;

		@Override
		public SerializableSparseArray<E> clone()
		{
			return (SerializableSparseArray<E>) super.clone();
		}
	}
}
